package com.ruoyi.place.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.place.domain.XlkjPlaceSurvey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.place.mapper.XlkjPlaceMapper;
import com.ruoyi.place.domain.XlkjPlace;
import com.ruoyi.place.service.IXlkjPlaceService;

/**
 * 行业场所Service业务层处理
 *
 * @author wang
 * @date 2022-01-12
 */
@Service
public class XlkjPlaceServiceImpl implements IXlkjPlaceService
{
    @Autowired
    private XlkjPlaceMapper xlkjPlaceMapper;

    /**
     * 查询行业场所
     *
     * @param id 行业场所主键
     * @return 行业场所
     */
    @Override
    public XlkjPlace selectXlkjPlaceById(Long id)
    {
        return xlkjPlaceMapper.selectXlkjPlaceById(id);
    }

    /**
     * 查询行业场所列表
     *
     * @param xlkjPlace 行业场所
     * @return 行业场所
     */
    @Override
    public List<XlkjPlace> selectXlkjPlaceList(XlkjPlace xlkjPlace)
    {
        return xlkjPlaceMapper.selectXlkjPlaceList(xlkjPlace);
    }

    /**
     * 新增行业场所
     *
     * @param xlkjPlace 行业场所
     * @return 结果
     */
    @Override
    public int insertXlkjPlace(XlkjPlace xlkjPlace)
    {
        xlkjPlace.setCreateTime(DateUtils.getNowDate());
        return xlkjPlaceMapper.insertXlkjPlace(xlkjPlace);
    }

    /**
     * 修改行业场所
     *
     * @param xlkjPlace 行业场所
     * @return 结果
     */
    @Override
    public int updateXlkjPlace(XlkjPlace xlkjPlace)
    {
        xlkjPlace.setUpdateTime(DateUtils.getNowDate());
        return xlkjPlaceMapper.updateXlkjPlace(xlkjPlace);
    }

    /**
     * 批量删除行业场所
     *
     * @param ids 需要删除的行业场所主键
     * @return 结果
     */
    @Override
    public int deleteXlkjPlaceByIds(Long[] ids)
    {
        return xlkjPlaceMapper.deleteXlkjPlaceByIds(ids);
    }

    /**
     * 删除行业场所信息
     *
     * @param id 行业场所主键
     * @return 结果
     */
    @Override
    public int deleteXlkjPlaceById(Long id)
    {
        return xlkjPlaceMapper.deleteXlkjPlaceById(id);
    }

    /**
     * 行业场所数据
     *
     * @param details
     */
    @Override
    public String importDevice(List<XlkjPlace> details) {
        if (StringUtils.isNull(details) || details.size() == 0){
            throw new CustomException("导入的行业场所数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (XlkjPlace user : details){
            try{
                this.insertXlkjPlace(user);
                successNum++;
                successMsg.append("导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "导入失败";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<XlkjPlace> selectPlaceByDepartmentId(String[] departmentId) {
        return xlkjPlaceMapper.selectPlaceByDepartmentId(departmentId);
    }

    @Override
    public XlkjPlaceSurvey selectPlaceNumyDeptId(Long deptId) {
        return xlkjPlaceMapper.selectPlaceNumyDeptId(deptId);
    }
}
